﻿using System.Collections.Generic;
using System.Linq;
using DTO;

namespace DAO
{
    public class Like_BaiTraLoiDAO
    {
        public static List<Like_BaiTraLoiDTO> getAll()
        {
            List<Like_BaiTraLoiDTO> result = new List<Like_BaiTraLoiDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LIKE_BAITRALOIs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                Like_BaiTraLoiDTO dto = new Like_BaiTraLoiDTO();

                dto.MaLike = ds.MaLike;
                dto.MaBaiTraLoi = ds.MaBaiTraLoi.Value;
                dto.MaNguoiDung = ds.MaNguoiDung.Value;
                dto.ThoiGianLike = ds.ThoiGianLike.Value;
                
                result.Add(dto);
            }

            return result;
        }

        public static Like_BaiTraLoiDTO getAlongCode(int maLike)
        {
            Like_BaiTraLoiDTO result = new Like_BaiTraLoiDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LIKE_BAITRALOIs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaLike == maLike).Single();

            result.MaLike = query.MaLike;
            result.MaBaiTraLoi = query.MaBaiTraLoi.Value;
            result.MaNguoiDung = query.MaNguoiDung.Value;
            result.ThoiGianLike = query.ThoiGianLike.Value;

            return result;
        }

        public static void Insert(Like_BaiTraLoiDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LIKE_BAITRALOI like = new LIKE_BAITRALOI();
            like.MaBaiTraLoi = _insert.MaBaiTraLoi;
            like.MaNguoiDung = _insert.MaNguoiDung;
            like.ThoiGianLike = _insert.ThoiGianLike;
            like.Deleted = _insert.Delete;           

            dbRaoVat.LIKE_BAITRALOIs.InsertOnSubmit(like);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maLike)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LIKE_BAITRALOI _delete = (from n in dbRaoVat.LIKE_BAITRALOIs
                                 where n.MaLike == maLike && n.Deleted == 0
                                 select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(Like_BaiTraLoiDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LIKE_BAITRALOI _update = (from n in dbRaoVat.LIKE_BAITRALOIs
                                      where n.MaLike == dto.MaLike && n.Deleted == 0
                                      select n).Single();

            _update.MaBaiTraLoi = dto.MaBaiTraLoi;
            _update.MaNguoiDung = dto.MaNguoiDung;
            _update.ThoiGianLike = dto.ThoiGianLike;
            
            dbRaoVat.SubmitChanges();
        }
    }
}
